/*
 * Copyrignt (c) xuzeshui.com. All Rights Reserved.
 * Author: Zeshui Xu<i@xuzeshui.com>
 * Created Time: 2016-08-26 20:00:09
 * Last Modified: 2016-08-26 20:50:50
 * File Name: controllers/filmvideoadmin.go
 * Description:
 */
package controllers

import (
	"encoding/json"

	"advertoper/errorcode"
	"advertoper/filmvideo"

	log "github.com/cihub/seelog"
)

type FilmVideoAdminController struct {
	SCLoginedController
}

func (p *FilmVideoAdminController) Prepare() {
	p.SCLoginedController.Prepare()
	p.ForceHTTPS()
}

// @router /create [post]
func (p *FilmVideoAdminController) CreateProgram() {
	defer p.Output("CreateProgram")

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body : %s", string(body))

	ti := &filmvideo.FileVideoInfo{}
	if err := json.Unmarshal(body, ti); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}
	err := filmvideo.InsertFileVideoInfo(ti)
	if err != nil {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = err.Error()
	} else {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	}
}

// @router /modify [post]
func (p *FilmVideoAdminController) ModifyProgram() {
	defer p.Output("ModifyProgram")

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body : %s", string(body))

	data := make(map[string]interface{})
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	err := filmvideo.UpdateFileVideoInfo(data)
	if err != nil {
		p.Resp["code"] = errorcode.EC_GL_INTERNAL_ERROR
		p.Resp["msg"] = err.Error()
	} else {
		p.Resp["code"] = errorcode.EC_GL_SUCCESS
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_SUCCESS)
	}
}

// @router /list [post]
func (p *FilmVideoAdminController) ListProgram() {
	defer p.Output("ListProgram")

	fis, _ := filmvideo.ListFileVideoInfo(0, 10000, nil, false)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
	p.Resp["data"] = fis
}

// @router /delete [post]
func (p *FilmVideoAdminController) DeleteProgram() {
	defer p.Output("DeleteProgram")

	body := p.Ctx.Input.RequestBody
	log.Infof("recv body : %s", string(body))
	data := make(map[string]interface{})
	if err := json.Unmarshal(body, &data); err != nil {
		p.Resp["code"] = errorcode.EC_GL_INVALID_JSON_BODY
		p.Resp["msg"] = errorcode.Msg(errorcode.EC_GL_INVALID_JSON_BODY)
		return
	}

	var mediaId int64
	if val, ok := data["media_id"]; ok {
		mediaId = int64(val.(float64))
	}

	filmvideo.DeleteFileVideoInfo(mediaId)
	p.Resp["code"] = 0
	p.Resp["msg"] = "success"
}
